#include<iostream>
#include<sys/timerfd.h> 
#include<unistd.h>


int main()
{
    int timerfd = timerfd_create(CLOCK_MONOTONIC,0); 
    if(timerfd < 0)
    {
        perror("timerfd create failed"); 
        return -1;
    }
    struct itimerspec itp;
    itp.it_value.tv_sec = 3; //第一次触发的时间
    itp.it_value.tv_nsec = 0;
    itp.it_interval.tv_sec = 1; //第一次超时后每隔多少秒触发一次
    itp.it_interval.tv_nsec = 0;
    timerfd_settime(timerfd,0,&itp,nullptr); 

    while(1)
    {
        uint64_t ret = 0;
        int n  = read(timerfd,&ret,8); 
        if(n < 0)
        {
            perror("read failed");
            break;
        }        
        printf("距离上次超时了%ld次\n",ret);
    }

    return 0;
}